//-----------------------------------------------------------------------------
//
// @file	Tests.h
// @brief	File with different kind of tests
// @author	Vinod Melapudi
// @date	20Feb2010
//
//-----------------------------------------------------------------------------

#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "assert.h"

#include <vector>
#include <list>

using namespace std;



// Test structure
struct SAMPLE1
{
	char	name[16];
	int		id;
	bool	valid;
}t;

struct SAMPLE2
{
	char		place[16];
	SAMPLE1		sample1;
	float		rate;
	int			num[10];
};


// Fn test if a c structure from memory can be written to file and then read directly into the structure
void TestBinaryReader()
{
	SAMPLE1 sample1_in;
	SAMPLE2 sample2_in1, sample2_in2, sample2_out1, sample2_out2;

	strcpy( sample1_in.name, "sample1 test1" );
	sample1_in.id = 20;
	sample1_in.valid = true;

	strcpy( sample2_in1.place, "sample2 test1" );
	sample2_in1.sample1 = sample1_in;
	sample2_in1.rate = 123.45;
	for( int i=0; i < 10; i++ )
		sample2_in1.num[i] = i*rand();


	strcpy( sample2_in2.place, "sample2 test2" );
	strcpy( sample2_in2.sample1.name, "sample1 test2" );
	sample2_in2.sample1.id = 21;
	sample2_in2.sample1.valid = false;
	sample2_in2.rate = 67.89;
	for( int i=0; i < 10; i++ )
		sample2_in2.num[i] = i*rand();


	assert( sizeof(struct SAMPLE2) == sizeof(sample2_in1) );


	// Test output file
	char output_file[] = "o_binary_test";
	FILE* file = fopen( output_file, "w" );
	fwrite((char *)&sample2_in1, sizeof(sample2_in1), 1, file);
	fwrite((char *)&sample2_in2, sizeof(SAMPLE2), 1, file);
	fclose( file );


	// Read the output file
	file = fopen( output_file, "r" );
	fread((char *)&sample2_out1, sizeof(sample2_out1), 1, file);
	fread((char *)&sample2_out2, sizeof(SAMPLE2), 1, file);
	fclose( file );

	assert( sample2_in1.sample1.id == sample2_out1.sample1.id );
	assert( sample2_in2.rate == sample2_out2.rate );
}

void main()
{
	// Fn test if a c structure from memory can be written to file and then read directly into the structure
	TestBinaryReader();
}